'use strict';

goog.provide('Blockly.Arduino.usb');
goog.require('Blockly.Arduino');

//模拟鼠标开启或关闭
Blockly.Arduino.forBlock['xnr_mouse_begin_end'] = function () {
    this.setTooltip("USB鼠标模拟开启或关闭");

    var dropdown_type = this.getFieldValue('type');
    Blockly.Arduino.definitions_['include_Mouse'] = '#include "Mouse.h"';
    var code = 'Mouse.' + dropdown_type + '();\n';
    return code;
};

//定义鼠标按键
Blockly.Arduino.forBlock['xnr_mouse_click_location'] = function () {
    var dropdown_location_type = this.getFieldValue('location_type');
    var code = dropdown_location_type;
    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

//操作鼠标，单击、按下、释放鼠标按键
Blockly.Arduino.forBlock['xnr_mouse_click'] = function () {
    this.setTooltip(
        "操作鼠标，单击、按下、释放鼠标按键"
    );

    var value_click_location = Blockly.Arduino.valueToCode(this, 'click_location', Blockly.Arduino.ORDER_ATOMIC);
    var dropdown_click_type = this.getFieldValue('click_type');
    var code = 'Mouse.' + dropdown_click_type + '(' + value_click_location + ');\n';
    return code;
};

//移动鼠标
Blockly.Arduino.forBlock['xnr_mouse_move'] = function () {
    this.setTooltip(
        "移动鼠标"
    );

    var value_move_x = Blockly.Arduino.valueToCode(this, 'move_x', Blockly.Arduino.ORDER_ATOMIC);
    var value_move_y = Blockly.Arduino.valueToCode(this, 'move_y', Blockly.Arduino.ORDER_ATOMIC);
    var value_move_wheel = Blockly.Arduino.valueToCode(this, 'move_wheel', Blockly.Arduino.ORDER_ATOMIC);
    var code = 'Mouse.move(' + value_move_x + ', ' + value_move_y + ', ' + value_move_wheel + ');\n';
    return code;
};

//检测鼠标按键是否被按下
Blockly.Arduino.forBlock['xnr_mouse_isPressed'] = function () {
    this.setTooltip("检测鼠标某个按键是否被按下，返回数据的类型为boolean");

    var value_type = Blockly.Arduino.valueToCode(this, 'type', Blockly.Arduino.ORDER_ATOMIC);
    var code = 'Mouse.isPressed(' + value_type + ')';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

//键盘模拟开启或关闭
Blockly.Arduino.forBlock['xnr_keyboard_begin_end'] = function () {
    this.setTooltip("USB键盘模拟开启或关闭");

    var dropdown_type = this.getFieldValue('type');
    Blockly.Arduino.definitions_['include_Keyboard'] = '#include "Keyboard.h"';
    var code = 'Keyboard.' + dropdown_type + '();\n';
    return code;
};

//定义键盘的按键类型
Blockly.Arduino.forBlock['xnr_keyboard_key'] = function () {
    var dropdown_key_type = this.getFieldValue('key_type');
    var code = dropdown_key_type;
    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

//操作键盘，按下或释放某个按键
Blockly.Arduino.forBlock['xnr_keyboard_press'] = function () {
    this.setTooltip("操作键盘，按下或释放某个按键");

    var value_key = Blockly.Arduino.valueToCode(this, 'key', Blockly.Arduino.ORDER_ATOMIC);
    var dropdown_type = this.getFieldValue('type');
    var code = 'Keyboard.' + dropdown_type + '(' + value_key + ');\n';
    return code;
};

Blockly.Arduino.forBlock['xnr_keyboard_press_return'] = function () {
    this.setTooltip("操作键盘，按下或释放某个按键，返回数据的类型为size_t(2字节)");

    var value_key = Blockly.Arduino.valueToCode(this, 'key', Blockly.Arduino.ORDER_ATOMIC);
    var dropdown_type = this.getFieldValue('type');
    var code = 'Keyboard.' + dropdown_type + '(' + value_key + ')';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

//操作键盘，释放键盘上所有按键
Blockly.Arduino.forBlock['xnr_keyboard_releaseAll'] = function () {
    this.setTooltip("操作键盘，释放键盘上所有按键");

    var code = 'Keyboard.releaseAll();\n';
    return code;
};

//键盘向电脑发送字符
Blockly.Arduino.forBlock['xnr_keyboard_write'] = function () {
    this.setTooltip("键盘向电脑发送字符");

    var value_write_data = Blockly.Arduino.valueToCode(this, 'write_data', Blockly.Arduino.ORDER_ATOMIC);
    var code = 'Keyboard.write(' + value_write_data + ');\n';
    return code;
};

Blockly.Arduino.forBlock['xnr_keyboard_write_return'] = function () {
    this.setTooltip("键盘向电脑发送字符，返回数据的类型为size_t(2字节)");

    var value_write_data = Blockly.Arduino.valueToCode(this, 'write_data', Blockly.Arduino.ORDER_ATOMIC);
    var code = 'Keyboard.write(' + value_write_data + ')';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

//键盘向电脑发送字符串
Blockly.Arduino.forBlock['xnr_keyboard_print'] = function () {
    this.setTooltip("键盘向电脑发送字符串");

    var value_print_data = Blockly.Arduino.valueToCode(this, 'print_data', Blockly.Arduino.ORDER_ATOMIC);
    var dropdown_print_type = this.getFieldValue('print_type');
    var code = 'Keyboard.' + dropdown_print_type + '(' + value_print_data + ');\n';
    return code;
};

Blockly.Arduino.forBlock['xnr_setup'] = function() {
    var statements_cshqy = Blockly.Arduino.statementToCode(this, 'xyt');
    Blockly.Arduino.setups_['xyt'] = statements_cshqy;
    var code = '';
    return code;
};

Blockly.Arduino.forBlock['serialBT_Init'] = function () {
    var content = Blockly.Arduino.valueToCode(this, 'CONTENT', Blockly.Arduino.ORDER_ATOMIC) || profile.default.serial;
    Blockly.Arduino.definitions_['include_SerialBT'] = '#include <SerialBT.h>';
    Blockly.Arduino.setups_['setup_serial_BT'] =  'SerialBT.begin(' + content + ');';
    return '';
};

Blockly.Arduino.forBlock['xnr_serialBT_SerialBT'] = function() {
    var code = 'SerialBT';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
  };
  
Blockly.Arduino.forBlock['xnr_serialBT_available'] = function() {
    var code = 'SerialBT.available()';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino.forBlock['xnr_serialBT_read'] = function() {
    var code = 'SerialBT.read()';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino.forBlock['xnr_serialBT_write'] = function() {
    var value_data = Blockly.Arduino.valueToCode(this, 'data', Blockly.Arduino.ORDER_ATOMIC);
    var code = 'SerialBT.write('
               + value_data
               + ');\n';
    return code;
  };

  Blockly.Arduino.forBlock['xnr_serialBT_init'] = function() {
    var value_value = Blockly.Arduino.valueToCode(this, 'value', Blockly.Arduino.ORDER_ATOMIC);
    Blockly.Arduino.definitions_['include_'+'SerialBT'] = '#include <SerialBT.h>';
    Blockly.Arduino.setups_['begin'] = 'SerialBT.begin('
                                       + value_value
                                       + ');';
    var code = '';
    return code;
  };